<template>
  <div>
    <!-- 选择地址模态框 2018/3/7 11:44 zhangQ -->
    <Form ref="dispatchForm" :model="dispatchForm" :label-width="80">
      <FormItem label="选择企业">
        <Select v-model="dispatchForm.userAddressId" filterable @on-change="changeAddress">
          <template v-for="item in userAddressList">
            <Option :value="item.id">{{item.companyName}}({{item.stationName}})</Option>
          </template>
        </Select>
      </FormItem>
      <FormItem label="收货地址" prop="outAddress">
        <Input v-model="dispatchForm.outAddress" style="width: 200px;"/>
      </FormItem>
      <FormItem label="联系人" prop="outContacts">
        <Input v-model="dispatchForm.outContacts" style="width: 200px;"/>
      </FormItem>
      <FormItem label="电话" prop="outMobile">
        <Input v-model="dispatchForm.outMobile" style="width: 200px;"/>
      </FormItem>
      <FormItem label="重量" prop="outWeight">
        <Input v-model="dispatchForm.outWeight" style="width: 200px;"/>
      </FormItem>
      <FormItem label="备注">
        <Input v-model="dispatchForm.memo" type="textarea" :autosize="{minRows: 2, maxRows: 10}" style="width: 200px;"/>
      </FormItem>
      <div class="xlinkModal-footer">
        <Button type="primary" @click="onOk">确认派单</Button>
      </div>
    </Form>
  </div>
</template>

<script>
  import {getUserAddressMoreInfo} from '@/api/backEnd/userAddress'
  import _ from 'lodash'

  export default {
    name: 'check-address-modal',
    data () {
      let dispatchForm = {
        outAddress: '', outContacts: '', outMobile: '', outWeight: '', memo: '', userAddressId: ''
      }
      let userAddressList = []
      let datas = {dispatchForm, userAddressList, getUserAddressMoreInfo}
      return datas
    },
    props: {
      dispatchDriverInfo: {
        type: Object,
        default () {
          return {}
        }
      },
      addressList: {
        type: Array,
        default () {
          return []
        }
      }
    },
    computed: {},
    methods: {
      onOk () {
        if (!this.dispatchForm.outAddress) {
          this.$Message.error('地址不能为空')
          return
        }
        this.$refs['dispatchForm'].validate((valid) => {
          if (valid) {
            this.$emit('dispatch', this.dispatchForm)
          } else {
          }
        })
      },
      changeAddress (val) {
        console.log('change')
        let currentAddressParam = _.find(this.userAddressList, {id: val})
        this.dispatchForm.userAddressId = currentAddressParam.id
        this.dispatchForm.outAddress = currentAddressParam.address
        this.dispatchForm.outContacts = currentAddressParam.linkman
        this.dispatchForm.outMobile = currentAddressParam.phone
      },
      queryData () {
        getUserAddressMoreInfo({
          pageIndex: 1,
          pageSize: 1000
        }).then(res => {
          if (res.data.isSuccess === 1) {
            console.log('地址加载成功!')
            console.log(res.data.data)
            this.$data.userAddressList = res.data.data
            console.log('企业不显示失败可能原因:', this.dispatchDriverInfo.userAddressId)
            this.dispatchDriverInfo.userAddressId && (this.dispatchForm.userAddressId = this.dispatchDriverInfo.userAddressId)
          } else {
            console.log('地址加载失败!')
          }
        })
      }
    },
    created () {
    },
    mounted () {
      console.log('dispatchDriverInfo', this.dispatchDriverInfo)
      this.dispatchDriverInfo.outAddress && (this.dispatchForm.outAddress = this.dispatchDriverInfo.outAddress)
      this.dispatchDriverInfo.outContacts && (this.dispatchForm.outContacts = this.dispatchDriverInfo.outContacts)
      this.dispatchDriverInfo.outMobile && (this.dispatchForm.outMobile = this.dispatchDriverInfo.outMobile)
      this.dispatchDriverInfo.outWeight && (this.dispatchForm.outWeight = this.dispatchDriverInfo.outWeight)
      this.dispatchDriverInfo.memo && (this.dispatchForm.memo = this.dispatchDriverInfo.memo)
      // this.dispatchDriverInfo.userAddressId && (this.dispatchForm.userAddressId = this.dispatchDriverInfo.userAddressId)
      console.log('dispatchForm', this.dispatchForm)
      this.queryData()
    },
    components: {}
  }
</script>

<style scoped>

</style>
